package com.product.magina.dingtalk.exception;


import javax.persistence.EntityNotFoundException;

import com.magina.common.exception.BusinessException;
import com.magina.common.response.Response;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class ExceptionHandlers {
    static final Log LOG = LogFactory.getLog(ExceptionHandlers.class);

    public ExceptionHandlers() {
    }

    @ExceptionHandler({IllegalArgumentException.class})
    @ResponseBody
    public Response<String> iae(Exception ex) {
        LOG.error("IllegalArgumentException", ex);
        return Response.fail("5000", ex.toString());
    }

    @ExceptionHandler({EntityNotFoundException.class})
    @ResponseBody
    public Response<String> enfe(EntityNotFoundException ex) {
        return Response.fail("4040", ex.toString());
    }

    @ExceptionHandler({com.magina.common.exception.BusinessException.class})
    @ResponseBody
    public Response<String> ae(BusinessException ex) {
        LOG.error("BusinessException", ex);
        return StringUtils.isEmpty(ex.getCode()) ? Response.fail(ex.getMessage()) : Response.fail(ex.getCode(), ex.getMessage());
    }

    @ExceptionHandler({Exception.class})
    @ResponseBody
    public Response<String> throwable(Exception exception) {
        LOG.error((Object)exception.getMessage(), exception);
        return Response.fail("5999", "msg_system_error", exception.toString());
    }
}
